package com.rambo.sad;

import com.google.common.collect.ImmutableList;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class DefaultAction {
    private static Logger logger = Logger.getLogger(DefaultAction.class);

    /**
      *Description:暴露在外的登录响应服务
      */
    @RequestMapping("/login.do")
    public String login(HttpSession httpSession, UserPO userPO) {
        logger.info("login name：" + userPO.getName() + ",pwd:" + userPO.getPasswd());

        //一些必要的系统前置工作.....
        httpSession.setAttribute("user", userPO);
        return "success";
    }

    /**
      *Description:系统内部获取用户服务
      */
    @RequestMapping("/getuser.do")
    public String getUserList(Model model,HttpSession httpSession) {
        ImmutableList<UserPO> userList = ImmutableList.of((UserPO) httpSession.getAttribute("user"));

        model.addAttribute("userList", userList);
        logger.info("start getuserList：" + userList.size());
        return "userList";
    }

    /**
      *Description:系统内部退出服务，清空 session对象，注销该 session
      */
    @RequestMapping("/exit.do")
    public String exit(HttpSession httpSession) {

        httpSession.removeAttribute("user");
        httpSession.invalidate();
        logger.info("start exit.do ......");
        return "login";
    }
}